package com.boyadegroot.divelog.utilities;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * From https://github.com/greenrobot/greenrobot-common
 */
public class DateUtils {
    /**
     * Calendar objects are rather expensive: for heavy usage it's a good idea to use a single instance per thread
     * instead of calling Calendar.getInstance() multiple times. Calendar.getInstance() creates a new instance each
     * time.
     */
    private static final class DefaultCalendarThreadLocal extends ThreadLocal<Calendar> {
        @Override
        protected Calendar initialValue() {
            return Calendar.getInstance();
        }
    }

    private static final ThreadLocal<Calendar> calendarThreadLocal = new DefaultCalendarThreadLocal();

    public static String getLongDateString(int year, int month, int day) {
        DateFormat longdateFormat = DateFormat.getDateInstance(DateFormat.LONG);
        Calendar calendar = calendarThreadLocal.get();
        calendar.clear();
        calendar.set(year, month - 1, day, 0, 0, 0);
        Date date = calendar.getTime();
        return longdateFormat.format(date);
    }

    public static String getLongDateString(long timestamp) {
        DateFormat longdateFormat = DateFormat.getDateInstance(DateFormat.LONG);
        Date date = new Date(timestamp * 1000);
        return longdateFormat.format(date);
    }

    public static String getTimeString(long timestamp) {
        DateFormat longtimeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM);
        Date date = new Date(timestamp * 1000);
        return longtimeFormat.format(date);
    }

    public static String getLongDateTimeString(long timestamp) {
        DateFormat longdatetimeFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM);
        Date date = new Date(timestamp * 1000);
        return longdatetimeFormat.format(date);
    }

    public static String getDurationString(long seconds, boolean showHours, boolean showSeconds) {
        if (showHours) {
            long hours = TimeUnit.SECONDS.toHours(seconds);
            seconds -= TimeUnit.HOURS.toSeconds(hours);
            long minutes = TimeUnit.SECONDS.toMinutes(seconds);
            if (showSeconds) {
                // HMS
                seconds -= TimeUnit.MINUTES.toSeconds(minutes);
                return String.format("%d:%02d:%02d", hours, minutes, seconds);
            } else {
                // HM
                return String.format("%d:%02d", hours, minutes);
            }
        } else {
            long minutes = TimeUnit.SECONDS.toMinutes(seconds);
            if (showSeconds) {
                // MS
                seconds -= TimeUnit.MINUTES.toSeconds(minutes);
                return String.format("%02d\'%02d\"", minutes, seconds);
            } else {
                // M
                return String.format("%02d\'", minutes);
            }
        }
    }

    public static Date getDate(int year, int month, int day) {
        Calendar calendar = calendarThreadLocal.get();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        return (calendar.getTime());
    }

    public static Date getTime(int hour, int minute, int second) {
        Calendar calendar = calendarThreadLocal.get();
        calendar.clear();
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);
        return (calendar.getTime());
    }

    public static long getTimestamp(Date date, Date time) {
        long datetime = date.getTime() + 3600000 + time.getTime();
        return (datetime / 1000);
    }

    public static long getTimestamp(int year, int month, int day) {
        Calendar calendar = calendarThreadLocal.get();
        calendar.clear();
        calendar.set(year, month - 1, day);
        return (calendar.getTimeInMillis() / 1000);
    }

    public static long getTimestamp(int year, int month, int day, int hour, int minute, int second) {
        Calendar calendar = calendarThreadLocal.get();
        calendar.clear();
        calendar.set(year, month - 1, day, hour, minute, second);
        return (calendar.getTimeInMillis() / 1000);
    }
}